package com.jp.knowledge.activity;

import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.google.gson.JsonObject;
import com.google.gson.internal.LinkedTreeMap;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.adapter.AttentionAdapter;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.fragment.AttentionDetailFragment;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.AttentionHead;
import com.jp.knowledge.model.HeadLine;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.util.JsonUtil;
import com.jp.knowledge.util.ResultSubscriber;
import com.jp.knowledge.view.TabHostWrapPagerView;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.glide.transformations.CropCircleTransformation;

/**
 * 关注详情
 */
public class AttentionDetailActivityold extends SlidingActivity implements ResultSubscriber.OnResultListener, View.OnClickListener {


    private final int NAV_LIST_CODE = 0x000001;//导航列表
    private final int HEAD_DATA_CODE = 0x000002;//头部信息
    @ViewInject(R.id.icon)
    private ImageView icon;


    @ViewInject(R.id.them_tab_nav)
    private RecyclerView recycleView;

    @ViewInject(R.id.view_page)
    private TabHostWrapPagerView pager;

    private String infoId;
    private int type;

    private List<HeadLine> headLines;
    private AttentionAdapter attentAdapter;

    private int lastPosition;//上一个选中位置
    @Override
    protected int getResourceId() {
        return R.layout.activity_attention_detail;
    }

    private String[] itemNames = new String[]{"主题", "产品", "公司", "人物", "频道"};

    @Override
    protected void init() {
        infoId = getIntent().getStringExtra("infoId");
        type = getIntent().getIntExtra("type", 2);
        int size = itemNames.length;
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        attentAdapter = new AttentionAdapter(null,mContext);
        recycleView.setLayoutManager(manager);
        recycleView.setAdapter(attentAdapter);
        recycleView.setItemAnimator(new DefaultItemAnimator());


        getHeadNav();
        getHeadData();
        
        
        getListData();
    }

    private void getListData() {
    }

    /**
     * 获取 导航
     */
    private void getHeadNav() {
        JsonObject param = new JsonObject();
        param.addProperty("type", type);
        HttpHelper.getInstance(mContext).subscribeNavList(param, NAV_LIST_CODE, this);
    }

    /**
     * 获取头部信息
     */
    private void getHeadData() {
        JsonObject param = new JsonObject();
        param.addProperty("id", infoId);
        param.addProperty("type", type);
        HttpHelper.getInstance(mContext).subscribeInfo(param, HEAD_DATA_CODE, this);
    }

    @Override
    public void onStart(int requestType) {

        loading.show();

    }

    @Override
    public void onCompleted(int requestType) {
        loading.cancel();

    }

    @Override
    public void onError(int requestType) {
        loading.cancel();
    }

    @Override
    public void onNext(IModel data, int requestType) {
        int code = data.getErrcode();

        if (requestType == HEAD_DATA_CODE) {
            if (code == 0) {
                AttentionHead headdata = JsonUtil.getInstance().mapToObject((LinkedTreeMap<String, Object>) data.getData(), AttentionHead.class);
//                toolBar.setTitle(headdata.getTitle());
                Glide.with(mContext).load(headdata.getIcon()).bitmapTransform(new CropCircleTransformation(mContext))
                        .fitCenter().placeholder(R.mipmap.cgsays_logo_min)
                        .into(icon);//new BlurTransformation(getContext(), 25), .override(30,30)
            } else {

            }
        } else if (requestType == NAV_LIST_CODE) {
            if (code == 0) {
                headLines = JsonUtil.getInstance().listMapToObject((ArrayList<LinkedTreeMap<String, Object>>) data.getData(), HeadLine.class);
                if(headLines!=null&&headLines.size()>0){
                    headLines.get(0).setCheck(true);//默认选中第一个
                    attentAdapter.update(headLines);
                    int size = headLines.size();
                    List<Fragment> list = new ArrayList<>();
                    for (int i = 0; i <size ; i++) {
                        AttentionDetailFragment fragment = new AttentionDetailFragment();
                        fragment.setLabel(headLines.get(i).getTypeId());
                        fragment.setType(type);
                        fragment.setInfoId(infoId);
                        list.add(fragment);
                    }
                    pager.init(recycleView,attentAdapter,getSupportFragmentManager(), list);
                }else {
                    ToasUtil.toast(mContext,"获取导航数据失败");
                }

            } else {

            }
        }
    }

    @Override
    public void onClick(View view) {

    }

    /**
     * 内部使用适配器用户导航滑动
     */

}
